<?php

namespace App\Http\Controllers\Laifei\Talent;

use App\Http\Controllers\Controller;
use App\Models\Talent;
use App\Models\Tools\AliOss;
use App\Models\Tools\Data;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class LaifeiTalentDetailController extends Controller
{

    public function index()
    {

        $all = $this->all;
        $talent_id = $all['talent_id'];

        try {
            $result = [];
            // 达人信息
            $talent = DB::table('talent')->where('talent_id', '=', $talent_id)->first();
            $talent = json_decode(json_encode($talent), true);
            $talent['status_name'] = Data::$talentStatus[$talent['status']];
            $talent['short_name'] = DB::table('dict_area')->where('id', '=', $talent['district'])->value('short_name');
            $result['talent'] = $talent;
            $result['talent']['channel_name'] = DB::table('explore_channel')->where('channel_id', '=', $talent['channel_id'])->value('channel_name');
            $result['talent']['verify_img'] = AliOss::getUrl($talent['verify_img']);
            $result['logs'] = Talent::talentLog($talent_id);

            // 工单日志
            $works = DB::table('explore_work as ew')
                ->leftJoin('shop', 'ew.shop_id', '=', 'shop.shop_id')
                ->where('ew.talent_id', '=', $talent_id)
                ->orderByDesc('ew.work_id')
                ->select('ew.work_id', 'ew.work_no', 'ew.receive_time', 'ew.shop_id', 'ew.status', 'shop.shop_name')
                ->get()
                ->map(function ($item) {
                    $item->status_name = Data::$exploreWorkStatus[$item->status];
                    return $item;
                })
                ->toArray();
            $result['works'] = $works;

            // 微信信息
            $user = DB::table('lottery_users')->where('we_id', '=', $talent['we_id'])->select('we_id', 'openid', 'nickname', 'phone')->first();
            $user = json_decode(json_encode($user), true);
            $result['user'] = $user;

            // 霸王餐
            $lottery = DB::table('lottery_users_draw as lud')
                ->leftJoin('lottery_draw_detail as ldd', 'lud.act_id', '=', 'ldd.act_id')
                ->leftJoin('lottery_activity as la', 'lud.act_id', '=', 'la.act_id')
                ->leftJoin('shop', 'la.shop_id', '=', 'shop.shop_id')
                ->leftJoin('lottery_goods as lg', 'la.goods_id', '=', 'lg.goods_id')
                ->where('lud.we_id', '=', $user['we_id'])
                ->orderByDesc('lud.created_at')
                ->select('la.act_id', 'la.status', 'lg.goods_name', 'shop.shop_name', 'lud.created_at', 'ldd.we_id as win_we_id')
                ->get()
                ->map(function ($item) use ($user) {
                    $item->published = Data::$lotteryActivityStatus[$item->status];
                    if ($item->status === Data::DATA_DB_LOTTERY_ACTIVITY_STATUS_AFTER) {
                        $item->published = $item->win_we_id === $user['we_id']?'已中奖':'未中奖';
                    }
                    if ($item->status === Data::DATA_DB_LOTTERY_ACTIVITY_STATUS_SUCCESS) {
                        $item->published = $item->win_we_id === $user['we_id']?'已使用':'已结束';
                    }
                    return $item;
                })
                ->toArray();
            $lottery = json_decode(json_encode($lottery), true);
            $result['lottery'] = $lottery;

            return $this->setReturnJson(0, 'success', $result);
        } catch (\Exception $e) {
            Log::error(LaifeiTalentDetailController::class, ['talent_id' => $talent_id, 'msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1, '查询失败', ['talent_id' => $talent_id, 'msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
        }


    }

}
